package cn.wgx.module.ent.service;

import cn.wgx.common.base.IBaseService;
import cn.wgx.common.entity.ent.DTOEntApp;
import cn.wgx.common.entity.ent.EntApp;
import cn.wgx.common.entity.ent.EntUserApp;
import cn.wgx.common.entity.ent.Enterprise;
import cn.wgx.common.entity.sys.SysApp;
import cn.wgx.common.security.client.config.entity.LoginAppUser;
import cn.wgx.common.security.util.SysUserUtil;
import cn.wgx.module.ent.mapper.EntAppMapper;
import cn.wgx.module.sys.service.AppService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class EntAppService extends IBaseService<EntAppMapper, EntApp> {
    @Autowired
    AppService appService;
    @Autowired
    EnterpriseService enterpriseService;
    @Autowired
    EntUserAppService entUserAppService;

    //包装列表
    public List<DTOEntApp> getEntApps(Page page, Wrapper<EntApp> queryWrapper) {
        LoginAppUser loginAppUser = SysUserUtil.getLoginAppUser(null);
        if (loginAppUser == null) {
            return null;
        }
        IPage<EntApp> e = getBaseMapper().selectPage(page, queryWrapper);
        List<DTOEntApp> dTOEntApps = new ArrayList<>();
        for (EntApp entApp : e.getRecords()) {
            dTOEntApps.add(wrap(entApp));
        }
        return dTOEntApps;
    }

    //包装通用
    public DTOEntApp wrap(EntApp entApp) {
        if (null == entApp) {
            return null;
        }
        DTOEntApp dtoEntApp = new DTOEntApp();
        BeanUtils.copyProperties(entApp, dtoEntApp);
        if (entApp.getApp_id() != null) {
            SysApp sysApp = appService.getById(entApp.getApp_id());
            if (sysApp != null) {
                dtoEntApp.setAppname(sysApp.getName());
            }
            Enterprise enterprise = enterpriseService.getById(entApp.getEnt_id());
            if (enterprise != null) {
                dtoEntApp.setEntname(enterprise.getName());
            }
        }
        return dtoEntApp;
    }

    public List<SysApp> getAppNotInEnt(Page<SysApp> page, Integer ent_id) {
        Page<SysApp> page1 = baseMapper.listAppNotInEnt(page, ent_id);
        return page1.getRecords();
    }

    //获取当前开通应用的企业人数
    public int getPeopleNum(Integer ent_id, Integer app_id) {
        QueryWrapper<EntUserApp> queryWrapper = new QueryWrapper();
        queryWrapper.eq("ent_id", ent_id).eq("app_id", app_id);
        List<EntUserApp> list = entUserAppService.list(queryWrapper);
        int i = 0;
        if (list != null) {
            i = list.size();
        }
        QueryWrapper<EntApp> queryWrapper1 = new QueryWrapper();
        queryWrapper1.eq("ent_id", ent_id).eq("app_id", app_id);
        EntApp one = getOne(queryWrapper1);
        one.setPeopleNum(i);
        return i;
    }
}
